<?
require("../common/auth.php");
require("../class/OrderRecord.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
switch($_REQUEST['from']){
	case "index_code":
		$url="http://localhost/open/qrMIX?did=".$_SESSION['memberID'];
		if($p=$_REQUEST['p']){
			$url.="&p=$p";
		}
		if($d=$_REQUEST['d']){
			$url.="&d=$d";
		}
		if($t=$_REQUEST['t']){
			$t=urlencode($t);
			$url.="&t=$t";
		}
		$code_img=file_get_contents($url);
		echo $code_img;
		exit;
	break;
	case "logout":
		session_destroy();
        if($_SERVER['SERVER_NAME']!='www.12301.cc'){
            exit('{"outcome":2,"link":"http://'.$_SERVER['SERVER_NAME'].($_SESSION['is_sub_domain']==1?'/new/d':'').'/dlogin.html"}');
        }
		exit('{"outcome":1}');
	break;
	case "order_cancel":
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord($ordernum,$s);
		$orders=$or->info();
		$concat = $_REQUEST['concat'] ? $_REQUEST['concat'] : 0;
		if($or->cancel($memberID,$concat)){
			$msg="您".$orders['mainOrder']->UUltitle.$orders['mainOrder']->UUttitle."的订单".(string)$orders['mainOrder']->UUordernum."已被取消。【票付通】";
			$s->Send_SMS_System_Fee((string)$orders['mainOrder']->UUordertel,$msg,(string)$orders['mainOrder']->UUaid,(string)$orders['mainOrder']->UUordernum);
			$out=array("outcome"=>1);
			if($_SESSION['dtype']==6){
				$daction="取消订单($ordernum)";
				$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
				$GLOBALS['le']->query($str);
			}
		}else{ 
			$out=array("outcome"=>-1);
		}
		echo json_encode($out);
		break;
	case "order_resend":
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord($ordernum,$s);
		$orders=$or->info();
		if((string)$orders['mainOrder']->UUremsg>=3){
			$out=array("outcome"=>-1,"msg"=>"短信重发次数不能超过2次");
		}else{
			if($or->resendMsg()){
				$out=array("outcome"=>1);
				if($_SESSION['dtype']==6){
					$daction="重发订单短信($ordernum)";
					$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
					$GLOBALS['le']->query($str);
				}
			}else{ 
				$out=array("outcome"=>-1);
			}
		}
		echo json_encode($out);
		break;
	case "order_alter":
		$out=array("outcome"=>1,"msg"=>"");
		$ordernum=$_REQUEST['ordernum'];
		$begintime=$_REQUEST['begintime'];
		$tids=$_REQUEST['tids'];
			
		$or=new OrderRecord($ordernum,$s);
		$info = $or->info();
		$info= (count($tids)>1) ? $or->info(0, 1):$or->info();
		if($info['mainOrder']->UUorder_confirm==5){
			 exit('{"outcome":-1,"msg":"您的订单已经修改过一次，无法再次修改"}');
		}
		$orders=array(0=>$info['mainOrder']);
		$orders=array_merge($orders,$info['childOrder']);
		
		/*修改游玩日期*/
		if(substr((string)$info['mainOrder']->UUbegintime,0,10)!=$begintime){
			require_once("../class/ProductInfo.php");
			foreach($orders as $row){
				$p=new ProductInfo($s,(string)$row->UUpid,(string)$row->UUaid);
				
				if($row->UUtprice!=$p->tPrice((string)$row->UUmid,$begintime))
					 exit('{"outcome":-1,"msg":"您选择的游玩日期与当前售价不同，不能修改！您可取消订单，重新下单"}');
			}
			if($begintime<substr((string)$info['mainOrder']->UUordertime,0,10))
				 exit('{"outcome":-1,"msg":"游玩日期不能小于下单时间"}');
			if(!$or->changeBegintime($begintime,$memberID))
				 exit('{"outcome":-1,"msg":"修改游玩日期失败"}');
			else	
				$out['msg'].="游玩日期修改成功 ";
		}
		
		/*修改门票数*/
		$allcancel=1;
		foreach($tids as $tid=>$newtnum){
			if($newtnum==0){
				exit('{"outcome":-1,"msg":"不能将订单门票数改为0，请通过取消操作"}');
			}
		}
		foreach($orders as $row){
			if($tids[(string)$row->UUtid]){
				if($row->UUtnum==$tids[(string)$row->UUtid]) continue;
				if($row->UUtnum<$tids[(string)$row->UUtid]){
					 exit('{"outcome":-1,"msg":"不能增加订单门票数"}');
				}
				if(!$or->changeTnum((string)$row->UUtid,$tids[(string)$row->UUtid],$memberID,$info)){
					exit('{"outcome":-1,"msg":"修改门票数失败"}');
				}else{
					$out['msg'].="门票数修改成功 ";	
				}
			}
		}
		if($out['msg']==""){
			$out['msg']="你未对订单做出变更";$out['outcome']=-1;
		}else{
			//$msg.="您".$info['mainOrder']->UUltitle.$info['mainOrder']->UUttitle."的订单".(string)$info['mainOrder']->UUordernum."已被修改。请查收新的短信";	//$s->Send_SMS_System_Fee((string)$info['mainOrder']->UUordertel,$msg,(string)$info['mainOrder']->UUaid,(string)$info['mainOrder']->UUordernum);
			if($_SESSION['dtype']==6){
				$daction="修改订单($ordernum)";
				$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
				$GLOBALS['le']->query($str);
			}
            // 查询是否发送短信

            $sql='SELECT sendVoucher FROM uu_land_f WHERE tid="'.(string)$info['mainOrder']->UUtid.'" LIMIT 1';
            $GLOBALS['le']->query($sql);
            $GLOBALS['le']->fetch_assoc();
            if(!$GLOBALS['le']->f('sendVoucher')){
                $or->resendMsg();
            }
		}
		echo json_encode($out);
		break;
	case "orderAlterHtml":
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord($ordernum,$s);
		$orders= ($_REQUEST['concat']==1)? $or->info(0,1):$or->info();
		if($orders['mainOrder']->UUp_type=='F'){// 套票只允许修改主票订单
			$orders['childOrder']=array();
		}
		
		?>
		
		<form class="changetnum" action="call/handle_pzp.php">
			<h3><?=$orders['mainOrder']->UUltitle?>-修改订单</h3>
			<p>(每个订单只能修改一次)</p>
			<input type="hidden" name="ordernum" value="<?=$orders['mainOrder']->UUordernum?>"/>
			<table>
				<tr>
					<th>游玩日期</th>
					<td><input type="text" class="inputT" name="begintime" value="<?=$orders['mainOrder']->UUbegintime?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/></td>
				</tr>
				<tr>
					<th><?=$orders['mainOrder']->UUttitle?></th>
					<td><input type="text" class="inputT" name="tids[<?=$orders['mainOrder']->UUtid?>]" value="<?=$orders['mainOrder']->UUtnum?>"/></td>
				</tr>
				<?foreach($orders['childOrder'] as $row){?>
				<tr>
					<th><?=$row->UUttitle?></th>
					<td><input type="text" class="inputT" name="tids[<?=$row->UUtid?>]" value="<?=$row->UUtnum?>"/></td>
				</tr>
				<?}?>
			</table>
			<div>
				<input type="submit" class="submit btn" value="确 定"/>
				<input type="button" class="cancel btn" value="取 消"/>
			</div>
		</form>
		<?
		break;
		
	case "ordertelHtml":
		$ordernum_a=$_REQUEST['ordernum_a'];
		$or=new OrderRecord($ordernum_a,$s);
		$orders= ($_REQUEST['concat']==1)? $or->info(0,1):$or->info();
		if($orders['mainOrder']->UUp_type=='F'){// 套票只允许修改主票订单
			$orders['childOrder']=array();
		}
		
		?>
		
		<form class="changetnum" action="javascript:void(0);" data="forward">
			<h3><?=$orders['mainOrder']->UUltitle?>-转发短信</h3>
			<p>(每个订单只能修改一次)</p>
			<input type="hidden" name="ordernum_a" value="<?=$orders['mainOrder']->UUordernum?>"/>
			<table>
				<tr>
					<th>转发的手机号码</th>
					<td><input type="text" class="inputT" name="tel" value=""/></td>
				</tr>
			</table>
			<div>
				<input type="submit" class="submit_a btn" value="确 定"/>
				<input type="button" class="cancel btn" value="取 消"/>
			</div>
		</form>
		<?
		break;
	case "order_forward": 
		
        // $ordernum=$_REQUEST['ordernum_a'];
		// $or=new OrderRecord($ordernum,$s);
		// $orders=$or->info();
		// if((string)$orders['mainOrder']->UUremsg>=3){
			// $out=array("outcome"=>-1,"msg"=>"短信重发次数不能超过2次");
		// }else{
			// if($or->resendMsg()){
				// $out=array("outcome"=>1);
				// if($_SESSION['dtype']==6){
					// $daction="重发订单短信($ordernum)";
					// $str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
					// $GLOBALS['le']->query($str);
				// }
			// }else{ 
				// $out=array("outcome"=>-1);
			// }
		// }
		// echo json_encode($out);
        break;
		
	case "withdraw_card"://添加提现账号
		if($bank_account1=trim($_REQUEST['bank_account1'])){
			$bank_account1=mysql_real_escape_string($bank_account1);
			$str="update pft_member set bank_account1='$bank_account1' where id=$memberID limit 1";
			$GLOBALS['le']->query($str);
		}
		if($bank_account2=trim($_REQUEST['bank_account2'])){
			$bank_account2=mysql_real_escape_string($bank_account2);
			$str="update pft_member set bank_account2='$bank_account2'  where id=$memberID limit 1";
			$GLOBALS['le']->query($str);
		}
		echo '{"outcome":1}';
		return true;
	case "withdraw_dele"://删除提现账号
		$bank_account=mysql_real_escape_string($_REQUEST['bankaccount']);
		$str="update pft_member set $bank_account='' where id=$memberID limit 1";
		$GLOBALS['le']->query($str);
		echo '{"outcome":1}';
		return true;
	break;
	case "withdraw_apply"://提现申请
		$money=abs($_REQUEST['money'])*100;
		include_once("../class/Member.php");
		$m=new Member($s,$memberID);
		$m_info=$m->info();
		$account=mysql_real_escape_string($_REQUEST['account']);
		if($account=="alipay"){
			$bank_name="支付宝";
			$bank_area_name="中国";
			$bank_accuont=$m_info['alipay'];
			$wd_name=$cardinfo[1];
		}else{
			$cardinfo=explode("|",$m_info[$account]);
			$bank_name=$cardinfo[0];
			$bank_area_name=$cardinfo[1];
			$bank_accuont=$cardinfo[2];
			$wd_name=$cardinfo[3];
		}
		$str="select id from pft_wd_cash where wd_status=0 AND fid={$m_info['id']}";
		$GLOBALS['le']->query($str);
		if($GLOBALS['le']->fetch_assoc()){
			exit('{"outcome":-1,"msg":"还有未完成的提现申请！"}');
		}
		if(!$bank_accuont){
			exit('{"outcome":-1,"msg":"提现账户不能为空"}');
		}
		if(($m->money()-$money)<0){
			exit('{"outcome":-1,"msg":"提现金额不能大于账户余额！"}');
		}
		
		
		
		$wd_money=$money;
		$bank_name.="(".$bank_area_name.")";
		$str="insert pft_wd_cash set fid=$memberID,wd_name='$wd_name',wd_money=$wd_money,apply_time=now(),bank_name='$bank_name',bank_accuont='$bank_accuont',batchno='$batchno',memo='提现'";
		if($GLOBALS['le']->query($str)){
			if($s->PFT_Member_Fund_Modify($memberID,$memberID,$wd_money,1,0,NULL,6)){
				exit('{"outcome":1}');
			}
		}
		exit('{"outcome":-1}');
		break;
	case "withdraw_cancel"://取消提现申请
		$id=abs($_REQUEST['id']);
		$str="select * from pft_wd_cash where id=$id";
		$GLOBALS['le']->query($str);
		$w_info=$GLOBALS['le']->fetch_assoc();
		if($w_info['fid']!=$memberID) exit('{"outcome":-1,"msg":"非本人提现，不能取消"}');
		$str="update pft_wd_cash set wd_status=3 where id=$id and fid=$memberID";
		$wd_money=$w_info['wd_money'];
		if($GLOBALS['le']->query($str)){
			if($s->PFT_Member_Fund_Modify($memberID,$memberID,$wd_money,0,0,NULL,6)){
				exit('{"outcome":1}');
			}
		}
		break;
	case "refund"://提现申请处理
		$id=abs($_REQUEST['id']);
		$str="select * from pft_wd_cash where id=$id";
		$GLOBALS['le']->query($str);
		$w_info=$GLOBALS['le']->fetch_assoc();
		if($_REQUEST['act']=="suc"){
			$str="update  pft_wd_cash set wd_status=1 where id=$id limit 1";
			if($GLOBALS['le']->query($str))	exit('{"outcome":1}');
		}elseif($_REQUEST['act']=="fail"){
			$memo=mysql_real_escape_string($_REQUEST['memo']);
			$wd_money=$w_info['wd_money'];
			$fid=$w_info['fid'];
			$str="update  pft_wd_cash set wd_status=2,memo='$memo' where id=$id limit 1";
			if($GLOBALS['le']->query($str)){
				if($s->PFT_Member_Fund_Modify($fid,$memberID,$wd_money,0,0,NULL,6)){
					exit('{"outcome":1}');
				}
			}
		}else{
			exit('{"outcome":-1,"msg":"未知操作"}');
		}
		break;
	case "settle_config"://结算配置
		$settleid=abs($_REQUEST['settleid']);
		$billdate=abs($_REQUEST['billdate']);
		if($billdate>31||$billdate<1){
			exit('{"outcome":-1,"msg":"账单日期非法"}');
		}
		$str="update uu_settle set billdate=$billdate where id=$settleid limit 1";
		$GLOBALS['le']->query($str);
		echo '{"outcome":1,"msg":"配置成功"}';
	break;
		case "bill_alter":
		$id=abs($_REQUEST['id']);
		$str="select * from uu_bill where id=$id";
		$GLOBALS['le']->query($str);
		$b_info=$GLOBALS['le']->fetch_assoc();
		if($_REQUEST['act']=="apply"){
			if($b_info['status']<>0){
				exit('{"outcome":-1,"msg":"你的账单已申请结算"}');
			}
			$str="update uu_bill set status=1 where id=$id limit 1";
			$GLOBALS['le']->query($str);
		}
		if($_REQUEST['act']=="cancel"){
			if($b_info['status']<>1){
				exit('{"outcome":-1,"msg":"结算中账单才能取消"}');
			}
			$str="update uu_bill set status=0 where id=$id limit 1";
			$GLOBALS['le']->query($str);
		}
		if($_REQUEST['act']=="complete"){
			if($b_info['status']==2){
				exit('{"outcome":-1,"msg":"账单已结算"}');
			}
			$str="update uu_bill set status=2 where id=$id limit 1";
			$GLOBALS['le']->query($str);
		}
		echo '{"outcome":1,"msg":"操作成功"}';
	break;
	case "orderView":
		$ordernum=$_REQUEST['ordernum'];

		$or=new OrderRecord($ordernum,$s);
		$orders=$or->info();
		if((string)$orders['mainOrder']->UUmid!=$memberID && (int)$orders['mainOrder']->UUaid!=$memberID &&$_SESSION['dtype']!=9){
			exit('{"outcome":-1,"msg":"你无查看凭证号权限"}');
		}
		if(is_numeric($outdate)){
			echo '{"outcome":-1,"msg":"获取凭证号失败"}';
		}else{
            $outdate=$s->Terminal_Code_Verify($ordernum,$memberID,1);
			$outdate=simplexml_load_string($outdate);
			$code=$outdate->Rec[0]->UUcode;
			//$msg="订单号：".$ordernum." 凭证号：".$code;
            $msg="&nbsp;".$code;
			$array=array("outcome"=>1,"msg"=>$msg);
			echo json_encode($array);
		}
	break;
	//手机页电子凭证中心获取凭证码
	case 'appVCode':
		$ordernum=$_REQUEST['ordernum'];
		$outdate=$s->Terminal_Code_Verify($ordernum,$memberID,1);
		$or=new OrderRecord($ordernum,$s);
		$orders=$or->info();
		echo $_SESSION['dtype'];
		exit;
		if((string)$orders['mainOrder']->UUmid!=$memberID&&$_SESSION['dtype']!=9){
			echo '你无查看凭证号权限';
		}
		if(is_numeric($outdate)){
			echo '获取凭证号失败';
		}else{
			$outdate=simplexml_load_string($outdate);
			$code=$outdate->Rec[0]->UUcode;
			echo $code;
		}
		break;
	default:
	exit("非法操作");

}
?>